package cn.ljy.authority.web.controller.backstage.authority;


import cn.ljy.authority.annotation.LoginUserToken;
import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.config.upload.FileUploadConfig;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.model.dto.SysUserBaseInformationDto;
import cn.ljy.authority.model.param.UserPasswordParam;
import cn.ljy.authority.service.SysPermissionService;
import cn.ljy.authority.service.SysUserService;
import cn.ljy.authority.web.controller.backstage.BaseController;
import cn.ljy.common.model.result.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api(tags = "后台个人账户模块")
@Slf4j
@Validated //加了后@NoNull之类的注解才能生效，如果只是@valid注解则不需要加上
@RestController
@RequestMapping(value = "/backstage/authority/user")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private FileUploadConfig fileUploadConfig;

    @ApiOperation(value = "获取当前登录账户信息")
//    @PreAuthorize("hasAuthority('sys:user:treemenu')")
    @GetMapping("/session")
    public ResponseData getAdmin(){
        //获取当前登陆账户
        return ResponseData.success(UserUtil.getLoginUser());
    }

    @ApiOperation(value = "获取当前账户的树形菜单")
//    @PreAuthorize("hasAuthority('sys:user:treemenu')")
    @GetMapping("/session/treeMenu")
    public ResponseData getTreeMenus(){
       //获取当前登陆账户的权限集合
//        List<SysPermission> permissions=UserUtil.getLoginUser().getSysPermissions();
        //List<SysPermission> list=adminMenuService.getTreeMenusByAdminId(admin.getId());//读取账户对应的树形菜单
        //return  ResponseData.success(sysPermissionService.getTreeMenus(permissions));
        return  ResponseData.success(sysPermissionService.getTreePermission(UserUtil.getLoginUser().getId(),true));
    }

    @ApiOperation(value = "修改当前账户的密码")
    @PatchMapping("/session/password")
    public ResponseData updatePassword(@Valid @RequestBody UserPasswordParam userPasswordParam, @LoginUserToken String token){
        // 获取当前登录用户
        SysUser loginUser = UserUtil.getLoginUser();
        sysUserService.updatePassword(loginUser.getUsername(),userPasswordParam.getPassword(), userPasswordParam.getNewPassword(), userPasswordParam.getConfirmPassword(),token);
        return ResponseData.success(null);
    }

    @ApiOperation(value = "上传个人头像")
    @PostMapping("/session/uploadPhoto")
    public ResponseData uploadImage(@NotNull(message="请选择上传文件") MultipartFile file){
        // 获取当前登录用户
        SysUser loginUser = UserUtil.getLoginUser();
        // 为该用户上传头像，并获取上传成功后的头像图片网址
        String fileName=sysUserService.uploadHeadImgFile(loginUser,file);
        return ResponseData.success(fileName);
    }


    @ApiOperation(value = "编辑个人账户")
    @PutMapping("/session")
    public ResponseData editSession(@Valid @RequestBody SysUserBaseInformationDto sysUserBaseInformationDto) {
        SysUser sysUser=new SysUser();
        BeanUtils.copyProperties(sysUserBaseInformationDto,sysUser);
        sysUserService.updateMySelf(sysUser);
        return ResponseData.success(null);
    }

}
